package com.sze.volanmenetrendgyor;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbConnector {
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;
	private Context mCtx;
	private static DbConnector mInstance;
	private static int opencount = 0;
	public static final String DATABASE_NAME = "DbVolanGyor";
	private static final int DATABASE_VERSION = 1;

	private static final String CREATE_LINES_TABLE = "Create Table LINES("
			+ "id integer primary key,"
			+ "name text,"
			+ "dir integer);";
	
	private static final String CREATE_STATIONS_TABLE = "Create Table STATIONS("
			+ "id integer primary key,"
			+ "name text,"
			+ "lat real,"
			+ "lng real);";

	private DbConnector(Context context) {
		mCtx = context;
	}

	public static DbConnector getInstance(Context context) {
		if (mInstance == null) {
			mInstance = new DbConnector(context);
		}
		return mInstance;
	}
	
	public synchronized void beginTransaction() {
		mDb.beginTransaction();
	}

	public synchronized void setTransactionSuccessful() {
		mDb.setTransactionSuccessful();
	}

	public synchronized void endTransaction() {
		mDb.endTransaction();
	}

	public synchronized void open() throws SQLException {
		if (opencount == 0) {
			mDbHelper = new DatabaseHelper(mCtx);
			mDb = mDbHelper.getWritableDatabase();
		}
		opencount++;
	}

	public synchronized void close() {
		opencount--;
		if (opencount == 0) {
			mDbHelper.close();
		}
	}

	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(CREATE_LINES_TABLE);
			db.execSQL(CREATE_STATIONS_TABLE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		}
	}

	public synchronized void replaceLinesData(LinesClass lc) {
		ContentValues values = new ContentValues();
		values.put("id", lc.id);
		values.put("name", lc.name);
		values.put("dir", lc.dir);
		mDb.replace("LINES", null, values);
	}
	public synchronized void replaceStationsData(StationClass sc) {
		ContentValues values = new ContentValues();
		values.put("id", sc.id);
		values.put("name", sc.name);
		values.put("lat", sc.lat);
		values.put("lng", sc.lng);
		mDb.replace("STATIONS", null, values);
	}

	public ArrayList<LinesClass> getLinesData() {
		String query = "SELECT * FROM LINES";
		Cursor cursor = mDb.rawQuery(query, null);
		cursor.moveToFirst();
		ArrayList<LinesClass> dataArray = new ArrayList<LinesClass>();
		while (!cursor.isAfterLast()) {
			LinesClass lc = new LinesClass();
			lc.id = cursor.getInt(cursor.getColumnIndex("id"));
			lc.name = cursor.getString(cursor.getColumnIndex("name"));
			lc.dir = cursor.getInt(cursor.getColumnIndex("dir"));
			
			dataArray.add(lc);
			cursor.moveToNext();
		}
		cursor.close();
		return dataArray;
	}
	
	public ArrayList<StationClass> getStationsData() {
		String query = "SELECT * FROM STATIONS";
		Cursor cursor = mDb.rawQuery(query, null);
		cursor.moveToFirst();
		ArrayList<StationClass> dataArray = new ArrayList<StationClass>();
		while (!cursor.isAfterLast()) {
			StationClass sc = new StationClass();
			sc.id = cursor.getInt(cursor.getColumnIndex("id"));
			sc.name = cursor.getString(cursor.getColumnIndex("name"));
			sc.lat = cursor.getFloat(cursor.getColumnIndex("lat"));
			sc.lng = cursor.getFloat(cursor.getColumnIndex("lng"));
			
			dataArray.add(sc);
			cursor.moveToNext();
		}
		cursor.close();
		return dataArray;
	}

}
